
import re
import os
import sys
import random
import shutil


class CopyToNew:
    prefex = ''
    filesPath = ''
    randomMax = 10

    def start(self):
        print('路径前缀')
        self.prefix = input().strip()
        print("原文件夹路径")
        self.filesPath = input().strip()
        print("输入 1 全完成路径 2 无路径 3 随机路径")
        type = input().strip()
        if type == '2':
            self.startNonePath()
        elif type == '3':
            self.StartrandomPath()
        else:
            self.startAllPath()

    def startAllPath(self):
        for root, dirs, files in os.walk(self.filesPath):
            for file in files:
                locals()
                path = os.path.join(root, file)
                bool = path.endswith((".h",".m",".swift"))
                print("path"+path)
                newRootPath = os.path.dirname(__file__) + '/NewCode' + self.prefix
                newFilePath = newRootPath + path.replace(self.filesPath,'')
                print('newFilePath:'+newFilePath)
                new_directory = os.path.dirname(newFilePath)
                if not os.path.exists(new_directory):
                    os.makedirs(new_directory)
                if bool == True:
                    os.chmod(path, 0o777)
                    codefile = open(path, 'r');
                    codeText = codefile.read()
                    # 将内容写入新文件
                    with open(newFilePath, 'w', encoding='utf-8') as f:
                        f.write(codeText)
                else:
                    shutil.copy(path, newFilePath)
    def startNonePath(self):
        for root, dirs, files in os.walk(self.filesPath):
            for file in files:
                locals()
                path = os.path.join(root, file)
                bool = path.endswith((".h", ".m", ".swift"))
                print("path" + path)
                newRootPath = os.path.dirname(__file__) + '/NewCode' + self.prefix
                newFilePath = newRootPath+'/'+ os.path.basename(path)
                print('newFilePath:' + newFilePath)
                new_directory = os.path.dirname(newFilePath)
                if not os.path.exists(new_directory):
                    os.makedirs(new_directory)
                if bool == True:
                    os.chmod(path, 0o777)
                    codefile = open(path, 'r');
                    codeText = codefile.read()
                    # 将内容写入新文件
                    with open(newFilePath, 'w', encoding='utf-8') as f:
                        f.write(codeText)
                else:
                    shutil.copy(path, newFilePath)
    def StartrandomPath(self):
        locals()
        methodNamePath = os.path.dirname(__file__) + '/methodName.txt'
        methodNamefile = open(methodNamePath, 'r');
        methodNameArray = methodNamefile.readlines()
        if (len(methodNameArray) == 0):
            print("error:路径下methodName.txt异常")
            return
        random_number = random.randint(1, self.randomMax)
        pathsArr = []
        for i in range(random_number):
            random_index = random.randint(0, len(methodNameArray) - 1)
            pathsArr.append(methodNameArray[random_index].replace('\n',''))
        for root, dirs, files in os.walk(self.filesPath):
            for file in files:
                locals()
                path = os.path.join(root, file)
                bool = path.endswith((".h", ".m", ".swift"))
                print("path" + path)
                newRootPath = os.path.dirname(__file__) + '/NewCode' + self.prefix+'/'
                r_index = random.randint(0, len(pathsArr) - 1)
                newFilePath = newRootPath + pathsArr[r_index] +'/'+ os.path.basename(path)
                print('newFilePath:' + newFilePath)
                new_directory = os.path.dirname(newFilePath)
                if not os.path.exists(new_directory):
                    os.makedirs(new_directory)
                if bool == True:
                    os.chmod(path, 0o777)
                    codefile = open(path, 'r');
                    codeText = codefile.read()
                    # 将内容写入新文件
                    with open(newFilePath, 'w', encoding='utf-8') as f:
                        f.write(codeText)
                else:
                    shutil.copy(path, newFilePath)



tool = CopyToNew()
tool.start()